package stat

import (
	"context"
	"github.com/hibiken/asynq"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/mr"
	"navi-mumbai/app/rpc/stat/pb"
	"navi-mumbai/app/timer/job/internal/svc"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/gamekey"
	"time"
)

type UpdateUserProvider1GameOrderDailyStatHandler struct {
	svcCtx *svc.ServiceContext
}

func NewUpdateUserProvider1GameOrderDailyStatHandler(svcCtx *svc.ServiceContext) *UpdateUserProvider1GameOrderDailyStatHandler {
	return &UpdateUserProvider1GameOrderDailyStatHandler{
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserProvider1GameOrderDailyStatHandler) ProcessTask(ctx context.Context, _ *asynq.Task) error {
	now := time.Now()
	loc, _ := time.LoadLocation(dbkey.DateTimeZone)
	st := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc).AddDate(0, 0, -1)
	et := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, loc).AddDate(0, 0, -1)
	err := mr.Finish(
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y01,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y02,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y03,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y04,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y05,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y06,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y07,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y08,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
		func() error {
			in := pb.UserGameDailyOrderStatReq{
				StartTime:  st.Format(dbkey.DateTimeCNFormat),
				EndTime:    et.Format(dbkey.DateTimeCNFormat),
				ProviderId: gamekey.ProviderIdY9Y,
				GameId:     gamekey.GameIdY9Y09,
			}
			_, err2 := l.svcCtx.StatSysRpc.UserGameDailyOrderStat(ctx, &in)
			if err2 != nil {
				logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err2)
				return nil
			}
			return nil
		},
	)
	if err != nil {
		logx.WithContext(ctx).Errorf("UserDailyGameOrderStat err: %v", err)
		return nil
	}

	return nil
}
